package cn.kgc.crowdfundingadmin.service;

import cn.kgc.crowdfundingadmin.mapper.CertMapper;
import cn.kgc.crowdfundingadmin.pojo.Cert;
import cn.kgc.crowdfundingadmin.pojo.CertExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CertServiceImpl implements  CertService {

    @Resource
    CertMapper certMapper;

    @Override
    public PageInfo<Cert> queryForKeywordWithPage(Integer pageNum, Integer pageSize, String keyword) {
        //开启分页功能
        PageHelper.startPage(pageNum,pageSize);
        //执行查询
        CertExample certExample=new CertExample();
        CertExample.Criteria criteria=certExample.createCriteria();
        criteria.andNameLike("%"+keyword+"%");
        List<Cert> list=certMapper.selectByExample(certExample);
        //分装为pageInfo对象
        return new PageInfo<>(list);
    }

    //根据id查询
    @Override
    public List<Cert> getCertListByIdList(List<Integer> certIdList) {
        //创建实体类Cert对应的Example对象
        CertExample certExample=new CertExample();
        //在Example对象中封装查询条件
        certExample.createCriteria().andIdIn(certIdList);
        //执行查询
        return certMapper.selectByExample(certExample);
    }

    //删除
    @Override
    public void batchRemove(List<Integer> certIdList) {
        //创建实体类Cert对应的Example对象
        CertExample certExample=new CertExample();
        //在Example对象中封装查询条件
        certExample.createCriteria().andIdIn(certIdList);

        certMapper.deleteByExample(certExample);
    }

    //添加
    @Override
    public void saveRole(String certName) {
        certMapper.insert(new Cert(null,certName));
    }

    @Override
    public void updateRole(Cert cert) {
        certMapper.updateByPrimaryKey(cert);
    }
}
